# -*- encoding=utf8 -*-
__author__ = "Administrator"

from airtest.core.api import *

auto_setup(__file__)

from airtest.core.api import *

auto_setup(__file__)
# -*- encoding=utf8 -*-
__author__ = "xiaoyun461"
from airtest.core.api import *
from airtest.aircv.aircv import *
from airtest.core.cv import *
from airtest.aircv.template import *
from airtest.utils.logger import get_logger
import random

auto_setup(__file__)
def height_width():
    global width
    global height
    global x_min
    global y_min
    global x_max
    global y_max
    global half_num
    if G.DEVICE.display_info['orientation'] in [1,3]:        
        height = G.DEVICE.display_info['width']        
        width = G.DEVICE.display_info['height']
    else:
        height = G.DEVICE.display_info['height']
        width = G.DEVICE.display_info['width']
    half_num = (0.5*width,0.5*height)
    x_min =width/4
    y_min =height/4
    x_max = width/4*3
    y_max = height/4*3
    return [height,width] 
def h_w_num(num_01,num_02):
    return (num_01*width,num_02*height)
def search_equal(sleep_num,like_num_in):    
    screen_01 = G.DEVICE.snapshot()  #截图到内存
    #图片裁切
    sleep(sleep_num)
    screen_02 = G.DEVICE.snapshot()
    predict_screen_01 = aircv.crop_image(screen_01, (x_min, y_min, x_max, y_max))
    predict_screen_02 = aircv.crop_image(screen_02, (x_min, y_min, x_max, y_max))
    like_num = find_template(predict_screen_01,predict_screen_02,-1)['confidence']
    LOGGING.debug("当前相似度 : %s" % like_num)
    if like_num >like_num_in:
        return True
    else:
        return False

def random_price(v_p):
    if exists(v_p):
        sleep(random.uniform(1,2))  
        v_p.target_pos=1
        if exists(v_p)==False:
            return False
        [min_x_one,min_y_one] = exists(v_p)
        v_p.target_pos=9
        if exists(v_p)==False:
            return False
        [max_x_nine,max_y_nine] = exists(v_p)    
        random_x = random.random()    
        random_y = random.random()
        num_x_tiaozhan =(max_x_nine-min_x_one)*random_x + min_x_one;
        num_y_tiaozhan =(max_y_nine-min_y_one)*random_y +min_y_one;
        LOGGING.debug("当前挑战随机坐标为 : （%s,%s）" % (num_x_tiaozhan,num_y_tiaozhan))
        touch(v =(num_x_tiaozhan,num_y_tiaozhan))
        return True
    else:
        return True
def start_yys(v_png):
    error_num = 0
    while True:
        error_num+=1
        LOGGING.debug("当前点击次数为 : %s 次" % (error_num))
        sleep(random.uniform(1,2))
        if error_num > 8:
            break
        num_x_n = random.random();
        num_y_n = random.random();
        v_random =((1+num_x_n)*(width*1/32),(1+num_y_n)*(height*3/8))
        LOGGING.debug("当前界面随机坐标为 : （%s,%s）" % (v_random))
        touch(v=v_random)
        if exists(v_png):
            error_num =0
            while True:
                if random_price(v_png):
                    break            
            sleep(random.uniform(8,10))
            while True:
                result_stop = search_equal(2,0.8)
                if result_stop:
                    break
height_width()
v_png =Template(r"tpl1606977855145.png", record_pos=(0.41, 0.219), resolution=(960, 540))

start_yys(v_png)



